const express = require('express');
const router = express.Router();
const adminServ = require('../../services/adminService');
const { asyncHandler } = require('../getSendResult');
const cryptor = require('../../util/crypt');

router.post(
  '/login',
  asyncHandler(async (req, res) => {
    const result = await adminServ.login(req.body.loginId, req.body.loginPwd);
    if (result) {
      let value = result.id;
      // 手动 加密
      value = cryptor.encrypt(value.toString());
      //登录成功
      // 浏览器 返回cookie
      res.cookie('token', value, {
        path: '/',
        domain: 'localhost',
        maxAge: 7 * 24 * 3600 * 1000, //毫秒数
        // signed: true,// 开启 对称加密
      });

      // 其他设备 返回authorization
      // 如果使用默认的加密 方法 这个也要 处理
      res.header('authorization', value);
    }
    return result;
  })
);

module.exports = router;
